package com.pgmmers.radar.dal.model.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pgmmers.radar.dal.bean.PageResult;
import com.pgmmers.radar.dal.bean.RuleHistoryQuery;
import com.pgmmers.radar.dal.bean.RuleQuery;
import com.pgmmers.radar.dal.model.RuleDal;
import com.pgmmers.radar.mapper.RuleHistoryMapper;
import com.pgmmers.radar.mapper.RuleMapper;
import com.pgmmers.radar.mapstruct.RuleHistoryMapping;
import com.pgmmers.radar.mapstruct.RuleMapping;
import com.pgmmers.radar.model.RuleHistoryPO;
import com.pgmmers.radar.model.RulePO;
import com.pgmmers.radar.util.BaseUtils;
import com.pgmmers.radar.vo.model.RuleHistoryVO;
import com.pgmmers.radar.vo.model.RuleVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class RuleDalImpl implements RuleDal {

    public static Logger logger = LoggerFactory.getLogger(RuleDalImpl.class);

    @Autowired
    private RuleMapper ruleMapper;

    @Autowired
    private RuleHistoryMapper ruleHistoryMapper;
    @Resource
    private RuleHistoryMapping ruleHistoryMapping;
    @Resource
    private RuleMapping ruleMapping;

    @Override
    public RuleVO get(Long id) {
        RulePO rule = ruleMapper.selectById(id);
        if (rule != null) {
            return ruleMapping.sourceToTarget(rule);
        }
        return null;
    }

    @Override
    public PageResult<RuleVO> query(RuleQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<RulePO> wrapper = Wrappers.lambdaQuery(RulePO.class);
        wrapper.eq(RulePO::getActivationId, query.getActivationId());
        if (!StringUtils.isEmpty(query.getName())) {
            wrapper.like(RulePO::getName, BaseUtils.buildLike(query.getName()));
        }
        if (query.getStatus() != null) {
            wrapper.eq(RulePO::getStatus, query.getStatus());
        }
//        RulePO rule = new RulePO();
//        rule.setActivationId(query.getActivationId());
//        rule.setStatus(query.getStatus());
//        rule.setName(query.getName());

        List<RulePO> list = ruleMapper.selectList(wrapper);
        Page<RulePO> page = (Page<RulePO>) list;
        List<RuleVO> listVO;
        if (CollectionUtils.isEmpty(list)) {
            listVO = new ArrayList<>();
        } else {
            listVO = ruleMapping.sourceToTarget(list);
        }

        return new PageResult<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), listVO);
    }

    @Override
    public int save(RuleVO rule) {
        RulePO rulePO = ruleMapping.targetToSource(rule);
        Date sysDate = new Date();
        int count = 0;
        if (rule.getId() == null) {
            rulePO.setCreateTime(sysDate);
            rulePO.setUpdateTime(sysDate);
            count = ruleMapper.insert(rulePO);
            rule.setId(rulePO.getId());// 返回id
        } else {
            rulePO.setUpdateTime(sysDate);
            count = ruleMapper.updateById(rulePO);
        }
        return count;
    }

    @Override
    public int delete(Long[] id) {
        int count = ruleMapper.delete(Wrappers.lambdaQuery(RulePO.class).in(RulePO::getId, List.of(id)));
        // TODO 删除关联子表
        return count;
    }

    @Override
    public PageResult<RuleHistoryVO> queryHistory(RuleHistoryQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<RuleHistoryPO> wrapper = Wrappers.lambdaQuery(RuleHistoryPO.class);
        wrapper.eq(RuleHistoryPO::getRuleId, query.getRuleId());
        List<RuleHistoryPO> list = ruleHistoryMapper.selectList(wrapper);
        Page<RuleHistoryPO> page = (Page<RuleHistoryPO>) list;

        List<RuleHistoryVO> listVO;
        if (CollectionUtils.isEmpty(list)) {
            listVO = new ArrayList<>();
        } else {
            listVO = ruleHistoryMapping.sourceToTarget(list);
        }
        return new PageResult<RuleHistoryVO>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), listVO);
    }

    @Override
    public int saveHistory(RuleHistoryVO ruleHistory) {
        RuleHistoryPO ruleHistoryPO = ruleHistoryMapping.targetToSource(ruleHistory);
        Date sysDate = new Date();
        int count = 0;
        ruleHistoryPO.setUpdateTime(sysDate);
        count = ruleHistoryMapper.insert(ruleHistoryPO);
        ruleHistory.setId(ruleHistoryPO.getId());// 返回id
        return count;
    }

    @Override
    public List<RuleVO> listByActId(Long actId) {
        List<RulePO> ruleList = ruleMapper.selectList(
                Wrappers.lambdaQuery(RulePO.class).eq(RulePO::getActivationId, actId)
        );
        return ruleMapping.sourceToTarget(ruleList);
    }

}
